package com.summer.hundred;

public class ques6 {
    public static void main(String[] args) {
        System.out.println(convert("Apalindromeisaword,phrase,number,orothersequenceofunitsthatcanbereadthesamewayineitherdirection,withgeneralallowancesforadjustmentstopunctuationandworddividers.", 10));
    }
    public static String convert(String s, int numRows) {
        int len = s.length();
        if(len<=numRows||numRows<=1) return s;
        int r =0 ;
        int num = 0;
        int l = 0;
        int c ;
        if ((numRows+(numRows-2))>len){
            c = 1;
            r = (len-numRows)+1;
        } else {
            c = len/(numRows+(numRows-2))+len%(numRows+(numRows-2))%numRows+1;
            r  = len/(numRows+(numRows-2))*(numRows-1)+len%(numRows+(numRows-2))%numRows+1;
        }
        Character[][] sa = new Character[numRows][r];
        char[] chars = s.toCharArray();
        for (int i = 0;i <c;i++){
            for (int k = 0;k<numRows&&l<r&&num<s.length();k++){
                sa[k][l] = chars[num++];
            //    sa[k][l] = s.charAt(num++);
            }
            l++;
            int n = numRows;
            for (int j = 0;j<numRows-2&&n>=0&&num<s.length();j++){
                sa[n-2][l++]=chars[num++];
                n--;
            }
        }
        String ss = "";
        for(int i = 0;i<numRows;i++){
            for (int j = 0; j <r ; j++) {
                if (sa[i][j]==null) continue;
                ss+=sa[i][j];
            }
        }
        return ss;
    }
}
